﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zanox.Api.Client.Auth.V20110501;
using Zanox.Api.Client.Data.V20110501;

namespace Zanox.Api.Client.Data.V20110501
{
    public class DataAPI
    {
        protected Zanox.Api.Client.Data.V20110501.DataService dataService;

        public DataAPI() : this(null) { }

        public DataAPI(System.Net.IWebProxy proxy)
        {
            #region zanox Publisher Web Services
            dataService = new Zanox.Api.Client.Data.V20110501.DataService();
            dataService.Proxy = proxy;
            dataService.EnableDecompression = true;
            dataService.UserAgent = Constants.UserAgent;
            #endregion
        }

        #region zanox Data Export
        public jobItem CreateReportJob(sessionType session, reportTypeEnum reportType, dateTypeEnum? dateType, DateTime fromData, DateTime toDate, string[] groupBy, string currency, int? programId, int? adspaceId, int? admediumId, int? admediumFormatId)
        {
            CreateReportJobRequest parameters = new CreateReportJobRequest();
            DateTime timestamp = DateTime.UtcNow;
            parameters.nonce = Signature.GetNonce();
            parameters.connectId = session.connectId;
            parameters.signature = Signature.SoapSignature(timestamp, session.secretKey, "dataservice", "createreportjob", parameters.nonce);
            parameters.timestamp = Signature.GetZDateTime(timestamp);

            parameters.reportType = reportType;

            if (dateType.HasValue)
            {
                parameters.dateType = dateType.Value;
                parameters.dateTypeSpecified = true;
            }
            parameters.fromDate = fromData;
            parameters.toDate = toDate;

            if (programId.HasValue)
            {
                parameters.programId = programId.Value;
                parameters.programIdSpecified = true;
            }
            if (admediumFormatId.HasValue)
            {
                parameters.admediumFormatId = admediumFormatId.Value;
                parameters.admediumFormatIdSpecified = true;
            }
            if (admediumId.HasValue)
            {
                parameters.admediumId = admediumId.Value;
                parameters.admediumIdSpecified = true;
            }

            if (adspaceId.HasValue)
            {
                parameters.adspaceId = adspaceId.Value;
                parameters.adspaceIdSpecified = true;
            }

            parameters.currency = currency;
            parameters.groupBy = groupBy;            

            CreateReportJobResponse response = dataService.CreateReportJob(parameters);

            if (response == null)
            {
                return null;
            }

            return response.jobItem;
        }

        /// <summary>
        /// Requires special permission by zanox
        /// </summary>        
        public jobItem CreateCashbackReportJob(sessionType session, reportTypeEnum reportType, DateTime fromData, DateTime toDate, int adspaceId)
        {
            CreateCashbackReportJobRequest parameters = new CreateCashbackReportJobRequest();
            DateTime timestamp = DateTime.UtcNow;
            parameters.nonce = Signature.GetNonce();
            parameters.connectId = session.connectId;
            parameters.signature = Signature.SoapSignature(timestamp, session.secretKey, "dataservice", "createcashbackreportjob", parameters.nonce);
            parameters.timestamp = Signature.GetZDateTime(timestamp);
            
            parameters.fromDate = fromData;
            parameters.toDate = toDate;
            parameters.adspaceId = adspaceId;

            CreateReportJobResponse response = dataService.CreateCashbackReportJob(parameters);

            if (response == null)
            {
                return null;
            }

            return response.jobItem;
        }

        public jobItem GetJob(sessionType session, string id)
        {
            GetJobRequest parameters = new GetJobRequest();
            DateTime timestamp = DateTime.UtcNow;
            parameters.nonce = Signature.GetNonce();
            parameters.connectId = session.connectId;
            parameters.signature = Signature.SoapSignature(timestamp, session.secretKey, "dataservice", "getjob", parameters.nonce);
            parameters.timestamp = Signature.GetZDateTime(timestamp);
            parameters.id = id;
            GetJobResponse response = dataService.GetJob(parameters);

            if (response == null)
            {
                return null;
            }

            return response.jobItem;
        }

        public IList<jobItem> GetJobs(sessionType session, DateTime? createData, jobTypeEnum? reportType, jobStatusTypeEnum? status, int items, int page, out int total)
        {
            GetJobsRequest parameters = new GetJobsRequest();
            DateTime timestamp = DateTime.UtcNow;
            parameters.nonce = Signature.GetNonce();
            parameters.connectId = session.connectId;
            parameters.signature = Signature.SoapSignature(timestamp, session.secretKey, "dataservice", "getjobs", parameters.nonce);
            parameters.timestamp = Signature.GetZDateTime(timestamp);

            parameters.items = items;
            parameters.page = page;

            if (createData.HasValue)
            {
                parameters.createDate = createData.Value;
                parameters.createDateSpecified = true;
            }

            if (reportType.HasValue)
            {
                parameters.type = reportType.Value;
                parameters.typeSpecified = true;
            }

            if (status.HasValue)
            {
                parameters.status = status.Value;
                parameters.statusSpecified = true;
            }

            GetJobsResponse response = dataService.GetJobs(parameters);

            if (response == null)
            {
                total = 0;
                return null;
            }

            if (response.jobItems == null)
            {
                total = 0;
                return null;
            }

            total = (int)response.total;
            return response.jobItems.ToList<jobItem>();
        }
        #endregion
    }
}
